package com.boco.modules.gdtp.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.utils.MenuUtils;
import com.boco.common.utils.StringUtils;
import com.boco.modules.gdtp.model.CommonResult;
import com.boco.modules.gdtp.vo.UserMenu;
import com.boco.sp.external.model.SysSystem;
import com.boco.sp.external.service.ISystemService;
import com.boco.sp.external.service.IUserService;
 
import org.springframework.beans.factory.annotation.Value;  
  /** 
   * 首页控制器
   * ClassName: IndexController <br/>
   * Function: TODO ADD FUNCTION. <br/>
   * Reason: TODO ADD REASON(可选). <br/>
   * date: 2017年3月10日 下午3:01:16 <br/>
   *
   * @author xups
   * @version 
   * @since JDK 1.7+
   */
@Controller
public class IndexController {
	
	private static Logger log = LoggerFactory.getLogger(PlatformsController.class);

	@Value("${server.id}")   
	public  Integer SERVER_ID; 
	
	@Resource
	private IUserService iUserProcess;
	@Resource
	private ISystemService iSystemService;
	
	@RequestMapping(value = "/index")
	public String index( HttpServletRequest request, HttpServletResponse response, Model model) {
		// 从单点登录后拿到用户名,从shiro获取的ObjectPrincipal不能直接转换成cas的AttributePrincipal
		String userName=getuserName(request);
		List<Map<String, Object>> userInfo=iUserProcess.searchUserInfoByUserName(userName);
		int userId;   
		if(userInfo==null){
			log.info("登录失败,未找到登录用户信息.");
			return null;
		}
		userId=(Integer)(userInfo.get(0).get("id"));
		
		List<SysSystem> systemList = iSystemService.getSystemByUserIdAndPlatformId(userId, SERVER_ID);
		model.addAttribute("sysName", "分级诊疗平台");
		model.addAttribute("systemList", systemList);
		model.addAttribute("userName",userName);
		return "/index";
	}
	

	@ResponseBody
	@RequestMapping(value="/getMenu",method=RequestMethod.GET)
	public CommonResult getSafetySpecificationMenu( HttpServletRequest
			request, HttpServletResponse response){
		Principal principal = request.getUserPrincipal();
		String sysId=request.getParameter("sysId");
		// 为空赋值为 admin，做测试用
		String userName="";
		if( principal==null){
			userName = "xinjiang01";
		}else{
			userName=principal.getName();
		}
		//根据用户名获取用户信息
		List<Map<String, Object>> userInfo=iUserProcess.searchUserInfoByUserName(userName);
		
		int userId;   
		if(userInfo==null){
			log.info("登录失败,未找到登录用户信息.");
			return null;
		}
		userId=(Integer)(userInfo.get(0).get("id"));
	
		List<Map<String, Object>> menuInfo = iUserProcess.serchResourcesByUserIdAndSystemId(userId,StringUtils.toInteger(sysId));
		
		List<UserMenu> userMenu=MenuUtils.resultMenu(menuInfo);
		CommonResult cr=new CommonResult();
		cr.setData(userMenu);
		return cr;
	}
	
	private String getuserName(HttpServletRequest request) {
		Principal principal = request.getUserPrincipal();
		String sysId=request.getParameter("sysId");
		// 为空赋值为 admin，做测试用
		String userName="";
		if( principal==null){
			userName = "xinjiang01";
		}else{
			userName=principal.getName();
		}
		return userName;
	}
}